﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2019-5-31
 * 时间: 9:55
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using O2S.Components.PDFView4NET;

namespace PdfViewer
{
	/// <summary>
	/// Description of PDFView.
	/// </summary>
	public partial class PDFView : UserControl
	{
		public PDFDocument PdfDocument;
		public PDFPageView PdfPageView;
		bool isChanging;
		
		public PDFView()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			PdfDocument = this.pdfDoc;
			PdfPageView = this.docView;
			isChanging = false;
		}
		
		public string LoadFilePath
		{
			set
			{
				LoadFile(value);
			}
		}
		
		//加载pdf文件
		private void LoadFile(string filepath)
		{
			try
			{
				string pdfPath = filepath;
//				string pdfPath = Path.GetDirectoryName(filepath) + "\\" + Path.GetFileNameWithoutExtension(filepath) + ".pdf";
//				switch (Path.GetExtension(filepath).ToLower())
//				{
//					case ".doc":
//						OfficeToPdf.DOCConvertToPDF(filepath, pdfPath);
//						break;
//					case ".xls":
//						OfficeToPdf.XLSConvertToPDF(filepath, pdfPath);
//						break;
//					case ".ppt":
//						OfficeToPdf.PPTConvertToPDF(filepath, pdfPath);
//						break;
//				}
				pdfDoc.Load(pdfPath);
				docView.Document = pdfDoc;
				tslbTotalPageCount.Text = pdfDoc.PageCount.ToString();
				tsddbtnCurrentPage.Items.Clear();
				for (int i = 0; i < pdfDoc.PageCount; i++)
					tsddbtnCurrentPage.Items.Add(i + 1);
				SetDragMode();
				isChanging = true;
				tsddbtnCurrentPage.SelectedIndex = 0;
				tscbShowRate.SelectedIndex = 4;
				isChanging = false;
			}
			catch (Exception ex)
			{
				MessageBox.Show("转换pdf错误"+ex.Message);
			}
		}
		
		void TsbtnDragClick(object sender, EventArgs e)
		{
			SetDragMode();
		}
		
		// 缩小
		void TsbtnZoomOutClick(object sender, EventArgs e)
		{
			SetZoomOutMode();
		}
		
		// 放大
		void TsbtnZoomInClick(object sender, EventArgs e)
		{
			SetZoomInMode();
		}
		
		void TsbtnFillWidthClick(object sender, EventArgs e)
		{
			isChanging = true;
			docView.ZoomMode = O2S.Components.PDFView4NET.PDFZoomMode.FitWidth;
			docView.ZoomMode = O2S.Components.PDFView4NET.PDFZoomMode.Custom;
			tscbShowRate.Text = docView.Zoom.ToString("f2") + "%";
			isChanging = false;
		}
		
		void TsbtnFillHeightClick(object sender, EventArgs e)
		{
			isChanging = true;
			docView.ZoomMode = O2S.Components.PDFView4NET.PDFZoomMode.FitHeight;
			docView.ZoomMode = O2S.Components.PDFView4NET.PDFZoomMode.Custom;
			tscbShowRate.Text = docView.Zoom.ToString("f2") + "%";
			isChanging = false;
		}
		
		void SetDragMode()
		{
			tsbtnDrag.Checked = true;
			tsbtnZoomIn.Checked = false;
			tsbtnZoomOut.Checked = false;
			docView.Cursor = Cursors.Hand;
			docView.WorkMode = UserInteractiveWorkMode.PanAndScan;
		}
		
		void SetZoomInMode()
		{
			tsbtnDrag.Checked = false;
			tsbtnZoomIn.Checked = true;
			tsbtnZoomOut.Checked = false;
			docView.Cursor = Cursors.Default;
			docView.WorkMode = UserInteractiveWorkMode.ZoomIn;
//			docView.ZoomMode = O2S.Components.PDFView4NET.PDFZoomMode.Custom;
		}
		
		void SetZoomOutMode()
		{
			tsbtnDrag.Checked = false;
			tsbtnZoomIn.Checked = false;
			tsbtnZoomOut.Checked = true;
			docView.Cursor = Cursors.Default;
			docView.WorkMode = UserInteractiveWorkMode.ZoomOut;
//			docView.ZoomMode = O2S.Components.PDFView4NET.PDFZoomMode.Custom;
		}
		
		void UpdatePageView()
		{
			int pageNum = 1;
			if (int.TryParse(tsddbtnCurrentPage.Text, out pageNum))
			{
				pageNum--;
				if (pageNum >= 0 && pageNum < pdfDoc.PageCount)
				{
					docView.PageNumber = pageNum;
				}
			}
		}
		
		void TsddbtnCurrentPageKeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				UpdatePageView();
			}
		}
		
		void TsddbtnCurrentPageSelectedIndexChanged(object sender, EventArgs e)
		{
			UpdatePageView();
		}
		
		void TscbShowRateSelectedIndexChanged(object sender, EventArgs e)
		{
			if (isChanging)
				return;
			isChanging = true;
			string str = tscbShowRate.Text.Trim();
			if (str.EndsWith("%"))
				str = str.Substring(0, str.Length - 1);
			double rate = 0;
			double.TryParse(str, out rate);
//			rate /= 100.0;
			docView.Zoom = rate;
			isChanging = false;
		}
		
		void TsbtnGotoFirstPageClick(object sender, EventArgs e)
		{
			tsddbtnCurrentPage.SelectedIndex = 0;
		}
		
		void TsbtnGotoPrePageClick(object sender, EventArgs e)
		{
			if (tsddbtnCurrentPage.SelectedIndex > 0)
				tsddbtnCurrentPage.SelectedIndex--;
		}
		
		void TsbtnGotoNextPageClick(object sender, EventArgs e)
		{
			if (tsddbtnCurrentPage.SelectedIndex < tsddbtnCurrentPage.Items.Count - 1)
				tsddbtnCurrentPage.SelectedIndex++;
		}
		
		void TsbtnGotoLastPageClick(object sender, EventArgs e)
		{
			tsddbtnCurrentPage.SelectedIndex = tsddbtnCurrentPage.Items.Count - 1;
		}
		
		void DocViewZoomChanged(object sender, EventArgs e)
		{
			isChanging = true;
			tscbShowRate.Text = docView.Zoom.ToString("f2") + "%";
			isChanging = false;
		}
	}
}
